import 'dart:math';
import 'package:flutter/material.dart';

class PersistentFanButton extends StatelessWidget {
  final Offset position;
  final ValueChanged<Offset> onDragUpdate;

  const PersistentFanButton({
    Key? key,
    required this.position,
    required this.onDragUpdate,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: position.dx,
      top: position.dy,
      child: GestureDetector(
        onPanUpdate: (details) {
          onDragUpdate(position + details.localPosition);
        },
        child: Stack(
          alignment: Alignment.center,
          children: [
            // Background fan shape
            CustomPaint(
              size: Size(200, 200),
              painter: FanPainter(),
            ),
            // Fan Buttons along the arc
            ..._buildFanButtons(),
            // Circular central icon
            Positioned(
              bottom: 10,
              child: CircleAvatar(
                backgroundColor: Colors.white,
                radius: 15,
                child: Icon(
                  Icons.menu,
                  color: Colors.black,
                  size: 20,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  List<Widget> _buildFanButtons() {
    const double radius = 75.0; // Radius for the arc
    const List<IconData> icons = [
      Icons.home, // 首页
      Icons.shopping_cart, // 商品
      Icons.lightbulb, // 方案
      Icons.settings, // 系统
    ];
    final List<Widget> fanButtons = [];

    for (int i = 0; i < icons.length; i++) {
      double angle = i * pi / 6; // Divide 90 degrees into 4 sections (i * pi/6 gives the angle)
      fanButtons.add(
        Positioned(
          left: radius + radius * cos(angle) - 20,
          top: radius - radius * sin(angle) - 20,
          child: FloatingActionButton(
            mini: true,
            onPressed: () {
              print("Button $i pressed");
            },
            child: Icon(icons[i], size: 18),
          ),
        ),
      );
    }

    return fanButtons;
  }
}

class FanPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.grey
      ..style = PaintingStyle.fill;

    final path = Path()
      ..moveTo(size.width / 2, size.height / 2)
      ..lineTo(size.width, size.height / 2)
      ..arcToPoint(
        Offset(size.width / 2, size.height),
        radius: Radius.circular(size.width / 2),
        clockwise: false,
      )
      ..close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: Stack(
        children: [
          // Example screen content
          PersistentFanButton(
            position: Offset(100, 100),
            onDragUpdate: (newPosition) {
              // Handle drag position update
            },
          ),
        ],
      ),
    ),
  ));
}
